/**图片处理*/
import { ref } from "vue";

const isOss = ref(import.meta.env.VITE_APP_ISOSS);
const url = ref(import.meta.env.VITE_APP_URL);
const imgViewUrl = ref(import.meta.env.VITE_APP_IMG_URL);

class ImgOssUtils {
  /** 处理图片*/
  loadImage(src, width = 80, height = 80) {
    if (!src) return "";
    src = String(src);
    if (isOss.value === "true") {
      //如果是 http开头的
      if (src.startsWith("http")) {
        // 判断是否是oss
        let oss =
          src.indexOf("aliyuncs.com") > 0 &&
          src.indexOf("oss-cn-") > 0 &&
          src.indexOf("x-oss-process") === -1;
        let prefix = src.indexOf("?") === -1 ? "?" : "&";
        if (oss && height) {
          return (
            src + prefix + "x-oss-process=image/resize,h_" + height + ",m_lfit"
          );
        }
        let cos = src.indexOf(".cos.") && src.indexOf("imageMogr2") === -1;
        if (cos && width && height) {
          return src + prefix + "imageMogr2/thumbnail/" + width + "x" + height;
        }
        return src;
      }

      console.log(
        `${url.value}${imgViewUrl.value}${src}?x-oss-process=image/resize,h_${height},m_lfit`
      );
      //如果是id
      if (/^\d+$/g.test(src) && height)
        return `${url.value}${imgViewUrl.value}${src}?x-oss-process=image/resize,h_${height},m_lfit`;
    } else {
      // 不开启 oss或者 cos 并且是 id
      if (/^\d+$/g.test(src)) return `${url.value}${imgViewUrl.value}${src}`;
      // 不开启 oss或者 cos 并且不是id
      return src;
    }
  }

  loadImage2(src) {
    if (!src) return "";
    if (/^\d+$/g.test(src)) return `${url.value}${imgViewUrl.value}${src}`;
  }
}

export default new ImgOssUtils();
